package repeat.personal.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * 
 */
public class DateUtils extends org.apache.commons.lang.time.DateUtils {

	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	
	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	
	public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
	
	public static final String YEAR_MONTH = "yyyyMM";
	
	public static final String YEAR_MONTH_DAY = "yyyyMMdd";
	
	public static final String YEAR_MONTH_DAY_HOUR_MINUTE_SECOND = "yyyyMMddHHmmss";
	
	public static final String HOUR_MINUTE_SECOND = "HH:mm:ss";

	public DateUtils() {
	}
	
	/**
	 * 获取两个时间相隔多少秒
	 * @param d1 比较大的值
	 * @param d2 比较小的值
	 * @return
	 */
	public static long getDateDiff(Date d1, Date d2) {
		return (d1.getTime() - d2.getTime()) / (1000);
	}
	
	/**
	 * 获取两个时间相隔多少毫秒秒
	 * @param d1 比较大的值
	 * @param d2 比较小的值
	 * @return
	 */
	public static long getDateDiffMillisecond(Date d1, Date d2) {
		return d1.getTime() - d2.getTime();
	}
	
	/**
	 * 下几秒
	 * @param amount
	 * @return
	 */
	public static Date getNextSecond(int amount) {
		return getNext(new Date(), Calendar.SECOND, amount);
	}
	
	/**
	 * 下几秒
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date getNextSecond(Date date, int amount) {
		return getNext(date, Calendar.SECOND, amount);
	}
	
	/**
	 * 下几分
	 * @param amount
	 * @return
	 */
	public static Date getNextMinute(int amount) {
		return getNextMinute(new Date(), amount);
	}
	
	/**
	 * 下几分
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date getNextMinute(Date date, int amount) {
		return getNext(date, Calendar.MINUTE, amount);
	}
	
	/**
	 * 下几小时
	 * @param amount
	 * @return
	 */
	public static Date getNextHour(int amount) {
		return getNext(new Date(), Calendar.HOUR, amount);
	}
	
	/**
	 * 下几小时
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date getNextHour(Date date, int amount) {
		return getNext(date, Calendar.HOUR, amount);
	}

    /**
     * 下几个月
     * @param date
     * @param amount
     * @return
     */
    public static Date getNextMonth(Date date, int amount) {
        return getNext(date, Calendar.MONTH, amount);
    }
	
	/**
	 * 获取某个日期
	 * @param date 日期
	 * @param field 
	 * @param amount
	 * @return
	 */
	public static Date getNext(Date date, int field, int amount) {
		Calendar calendar = getCalendar(date);
		calendar.add(field, amount);
		return calendar.getTime();
	}
	
	/**
	 * 按自定义日期模式转换日期字符并返回Date类型.
	 * 
	 * @param dateString 日期字符
	 * @param pattern 日期转换模式,例子:yyyy/MM/dd HH:mm:ss
	 * @return Date 返回日期(Date)类型
	 */
	public static Date stringToDate(String dateString, String pattern) {
		Date date = null;
		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		try {
			date = dateFormat.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 按自定义日期模式转换Date类型,并返回日期字符串.
	 * 
	 * @param date 日期(Date)类型
	 * @param pattern 日期转换模式,例子:yyyy/MM/dd HH:mm:ss
	 * @return String 返回日期字符�?
	 */
	public static String dateToString(Date date, String pattern) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		return dateFormat.format(date);
	}

	/**
	 * 将Date类型转换为Calendar类型,并返
	 * 
	 * @param date 日期(Date)类型
	 * @return Calendar 返回日历(Calendar)类型
	 */
	public static Calendar getCalendar(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}

	/**
	 * 根据日期(Date)类型,日历获得该域
	 * 
	 * @param date 日期(Date)类型
	 * @param field 日历例子:Calendar.YEAR
	 * @return int 返回域
	 */
	public static int get(Date date, int field) {
		Calendar calendar = getCalendar(date);
		return calendar.get(field);
	}

	/**
	 * 
	 * @param date
	 * @return int
	 */
	public static int getYear(Date date) {
		return get(date, Calendar.YEAR);
	}

	/**
	 * 
	 * @param date
	 * @return int
	 */
	public static int getMonth(Date date) {
		return get(date, Calendar.MONTH) + 1;
	}

	/**
	 * 
	 * @param date
	 * @return int
	 */
	public static int getDay(Date date) {
		return get(date, Calendar.DATE);
	}

	/**
	 * 
	 * @param date
	 * @param field
	 * @param amount
	 * @return Date
	 */
	public static Date rollDateByField(Date date, int field, int amount) {
		Calendar calendar = getCalendar(date);
		calendar.add(field, amount);
		return calendar.getTime();
	}

	/**
	 * 
	 * @param date
	 * @return Date
	 */
	public static Date getPreYear(Date date) {
		return rollDateByField(date, Calendar.YEAR, -1);
	}

	/**
	 * 
	 * @param date
	 * @return Date
	 */
	public static Date getPreMonth(Date date) {
		return rollDateByField(date, Calendar.MONTH, -1);
	}

	/**
	 * 
	 * @param date
	 * @param pattern
	 * @return String
	 */
	public static String getPreMonthToString(Date date, String pattern) {
		Date preDate = getPreMonth(date);
		return dateToString(preDate, pattern);
	}

	/**
	 * 
	 * @param date
	 * @return String
	 */
	public static String getPreMonthToString(Date date) {
		Date preDate = getPreMonth(date);
		return dateToString(preDate, DateUtils.YEAR_MONTH);
	}

	/**
	 * 
	 * @param date
	 * @return Date
	 */
	public static Date getPreDay(Date date) {
		return rollDateByField(date, Calendar.DATE, -1);
	}
	
   /**
	*  yyyy-MM-dd HH:mm:ss 格式字符转换 yyyyMMdd格式
	*/
	public static String stringToString(String str) {
		SimpleDateFormat dataFormat1 = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		SimpleDateFormat dataFormat2 = new SimpleDateFormat(YEAR_MONTH_DAY);
		String dataFormat = "";
		try {
			dataFormat = dataFormat2.format(dataFormat1.parse(str));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dataFormat;
	}
	
	/**
	 * 把时间类型date 转换为 日期类型  原 2018-01-01 18:00:00 转换为 2018-01-01
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static Date dateToDate(Date date, String pattern) {
		SimpleDateFormat dateFormat1 = new SimpleDateFormat(pattern);
		
		String dateString = dateFormat1.format(date);
		
		Date date1 = null;
		SimpleDateFormat dateFormat2 = new SimpleDateFormat(pattern);
		try {
			date1 = dateFormat2.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date1;
		
	}
	/** 
     * 计算两个日期之间相差的天数 
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
	 * @throws ParseException 
     */  
    public static int getDaysBetween(Date smdate,Date bdate)
    {  
        Calendar cal = Calendar.getInstance();  
        cal.setTime(smdate);  
        long time1 = cal.getTimeInMillis();               
        cal.setTime(bdate);  
        long time2 = cal.getTimeInMillis();       
        long between_days=(time2-time1)/(1000*3600*24);
          
       return Integer.parseInt(String.valueOf(between_days));         
    }  
    
    /**
     * 获取日期为当前的第几周
     * @param date
     * @return
     */
    public static int getWeek(Date date){ 
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(date);
        int weekNum = calendar.get(Calendar.WEEK_OF_YEAR);
        return weekNum;
    }
    
    /**
     * 获取字符串日期为当前的第几周
     * @param dateStr
     * @return
     * @throws ParseException 
     */
    public static int getWeekByStrDate(String dateStr){ 
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    	Date date = null;
		try {
			date = format.parse(dateStr);
		} catch (ParseException e) { 
			e.printStackTrace();
			return 0;
		}
        return getWeek(date);
    }

    public static void main(String[] args) {
        Date nextMonth = getNextMonth(new Date(), 3);
        String s = DateUtils.dateToString(nextMonth, DateUtils.YYYY_MM_DD_HH_MM_SS);
        System.out.println(s);
    }

}
